
package com.shanghaionstar.user.presenter;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.more.response.ChangePassResponse;
import com.shanghaionstar.R;
import com.shanghaionstar.user.iview.IChangePasswordView;
import com.shanghaionstar.user.service.ChangePasswordService;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.weight.CommonInputLayout;

/**
 * Created by 7828wc on 16/8/22.
 */
public class ChangePasswordPresenter extends BasePresenter<ChangePassResponse> {
    private ChangePasswordService service;
    private IChangePasswordView changePasswordView;

    public ChangePasswordPresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<ChangePassResponse> view) {
        super.setView(view);
        service = new ChangePasswordService(view.onActivity());
        this.changePasswordView = (IChangePasswordView) view;
        setServiceProvider(service);
    }

    public void setUpdatePassInfo(String newPass, String oldPass) {
        service.setUpdatePassInfo(newPass, oldPass);
    }

    public boolean verifyInput(CommonInputLayout inputLayout, boolean isOldPassword) {
        if (CommonUtil.isNull(inputLayout.getText())) {
            changePasswordView.verifyResult(inputLayout, R.string.input_user_info_not_null);
            return false;
        }
        if (!CommonUtil.passwordValidate(inputLayout.getText())) {
            changePasswordView.verifyResult(inputLayout, R.string.userpass_error);
            return false;
        }
        if (isOldPassword) {
            if (inputLayout.getText().equalsIgnoreCase(changePasswordView.oldPassword())) {
                changePasswordView.verifyResult(inputLayout, R.string.new_password_not_oldpass);
                return false;
            }
        }
        changePasswordView.verifyResult(inputLayout, FinalConstants.Register.NO_ERROR);
        return true;
    }

}

